package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.ExportExample;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.service.cargo.PackingService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(timeout = 200000)
public class PackingServiceImpl implements PackingService {

    @Autowired
    private PackingDao packingDao;

    @Autowired
    private ExportDao exportDao;

    /**
     * 分页查询
     * @param packingExample
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Packing> findByPage(PackingExample packingExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Packing> packingList = packingDao.selectByExample(packingExample);
        PageInfo<Packing> pageInfo = new PageInfo<>(packingList);
        return pageInfo;
    }

    @Override
    public void save(Packing packing) {
        packing.setPackingListId(UUID.randomUUID().toString());
        packing.setState(0);
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andIdIn(Arrays.asList(packing.getExportIds().split(",")));
        List<Export> exportList = exportDao.selectByExample(exportExample);
        if (exportList!=null && exportList.size() > 0){
            Export exportFilrst = exportList.get(0);
            packing.setShipmentPort(exportFilrst.getShipmentPort());
            packing.setDestinationPort(exportFilrst.getDestinationPort());
            packing.setConsignee(exportFilrst.getConsignee());
            for (Export export : exportList) {
                export.setState(4);
                exportDao.updateByPrimaryKeySelective(export);
            }
        }

        packingDao.insert(packing);
    }

    @Override
    public void update(Packing packing) {
        packingDao.updateByPrimaryKeySelective(packing);
    }

    @Override
    public Map<String, Object> delete(String packingListId) {
        Map<String,Object> map = new HashMap<>();
        Packing packing = packingDao.selectByPrimaryKey(packingListId);
        if (packing.getState() != 0) {
            map.put("message",0);
        } else {
            ExportExample exportExample = new ExportExample();
            exportExample.createCriteria().andIdIn(Arrays.asList(packing.getExportIds().split(",")));
            List<Export> exportList = exportDao.selectByExample(exportExample);
            if (exportList!=null && exportList.size()>0) {
                for (Export export : exportList) {
                    export.setState(2);
                    exportDao.updateByPrimaryKeySelective(export);
                }
            }
            packingDao.deleteByPrimaryKey(packingListId);
            map.put("message",1);
        }
        return map;
    }

    @Override
    public Packing findById(String packingListId) {
        return packingDao.selectByPrimaryKey(packingListId);
    }
}
